WOLF core
WOLF: A Modular Estimation Framework for Robotics
XXXXXXXXXXXXXXXXXXXXXXXXvoid(0)" frameborder="0" name="MSearchResults" id="MSearchResults">
covariance.h
Go to the documentation of this file.
1// WOLF - Copyright (C) 2020-2026
2// Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
3// Authors: Joan Solà Ortega (jsola@iri.upc.edu) and
4// Joan Vallvé Navarro (jvallve@iri.upc.edu)
5// All rights reserved.
6//
7// This file is part of WOLF: http://www.iri.upc.edu/wolf
8// WOLF is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License version 3
10// as published by the Free Software Foundation.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19#pragma once
20
21#include <Eigen/Dense>
22
23namespace wolf
24{
27template <typename T, int N, int RC>
28inline bool isSymmetric(const Eigen::Matrix<T, N, N, RC>& M, const T eps = wolf::Constants::EPS)
29{
30 if (M.cols() != M.rows()) return false;
31 return M.isApprox(M.transpose(), eps);
32}
33
36template <typename T, int N, int RC>
37inline bool isPositiveSemiDefinite(const Eigen::Matrix<T, N, N, RC>& M, const T& eps = Constants::EPS)
38{
39 Eigen::SelfAdjointEigenSolver<Eigen::Matrix<T, N, N, RC> > eigensolver(M, Eigen::EigenvaluesOnly);
40
41 if (eigensolver.info() == Eigen::Success)
42 {
43 // All eigenvalues must be >= 0:
44 return (eigensolver.eigenvalues().array() >= eps).all();
45 }
46 else
47 std::cout << "eigen decomposition failed" << std::endl;
48
49 return false;
50}
51
54template <typename T, int N, int RC>
55inline bool isCovariance(const Eigen::Matrix<T, N, N, RC>& M, const T& eps = Constants::EPS)
56{
57 return isSymmetric(M) && isPositiveSemiDefinite(M, eps);
58}
59
60#define WOLF_ASSERT_COVARIANCE_MATRIX(x) assert(isCovariance(x, Constants::EPS_SMALL) && "Not a covariance");
61
62#define WOLF_ASSERT_INFORMATION_MATRIX(x) assert(isCovariance(x, double(0.0)) && "Not an information matrix");
63
67template <typename T, int N, int RC>
68inline bool makePosDef(Eigen::Matrix<T, N, N, RC>& M, const T& eps = Constants::EPS)
69{
70 Eigen::SelfAdjointEigenSolver<Eigen::Matrix<T, N, N, RC> > eigensolver(M);
71
72 if (eigensolver.info() == Eigen::Success)
73 {
74 // All eigenvalues must be >= 0:
75 double epsilon = eps;
76 while ((eigensolver.eigenvalues().array() < eps).any())
77 {
78 // std::cout << "----- any negative eigenvalue or too close to zero\n";
79 // std::cout << "previous eigenvalues: " << eigensolver.eigenvalues().transpose() << std::endl;
80 // std::cout << "previous determinant: " << M.determinant() << std::endl;
81 M = eigensolver.eigenvectors() * eigensolver.eigenvalues().cwiseMax(epsilon).asDiagonal() *
82 eigensolver.eigenvectors().transpose();
83 eigensolver.compute(M);
84 // std::cout << "epsilon used: " << epsilon << std::endl;
85 // std::cout << "posterior eigenvalues: " << eigensolver.eigenvalues().transpose() << std::endl;
86 // std::cout << "posterior determinant: " << M.determinant() << std::endl;
87 epsilon *= 10;
88 }
90
91 return epsilon != eps;
92 }
93 else
94 WOLF_ERROR("Couldn't compute covariance eigen decomposition");
95
96 return false;
97}
98
102inline Eigen::MatrixXd computeSqrtUpper(const Eigen::MatrixXd& _info)
103{
104 // impose symmetry
105 Eigen::MatrixXd info = _info.selfadjointView<Eigen::Upper>();
106
107 // Normal Cholesky factorization
108 Eigen::LLT<Eigen::MatrixXd> llt_of_info(info);
109 Eigen::MatrixXd R = llt_of_info.matrixU();
110
111 // Good factorization
112 if (info.isApprox(R.transpose() * R, Constants::EPS)) return R;
113
114 // Not good factorization: SelfAdjointEigenSolver
115 Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> es(info);
116 Eigen::VectorXd eval = es.eigenvalues().real().cwiseMax(Constants::EPS);
117
118 R = eval.cwiseSqrt().asDiagonal() * es.eigenvectors().real().transpose();
119
120 return R;
121}
122
123} // namespace wolf
#define WOLF_ASSERT_COVARIANCE_MATRIX(x)
Definition: covariance.h:60
#define WOLF_ERROR(...)
Definition: logging.h:520
const double EPS
Definition: wolf.h:74
Definition: capture_base.h:23
bool isCovariance(const Eigen::Matrix< T, N, N, RC > &M, const T &eps=Constants::EPS)
Returns whether a matrix is a covariance: symmetric and positive semi-definite.
Definition: covariance.h:55
Eigen::MatrixXd computeSqrtUpper(const Eigen::MatrixXd &_info)
Computes the upper triangular square root R of an information matrix (RᵀR = info),...
Definition: covariance.h:102
bool isSymmetric(const Eigen::Matrix< T, N, N, RC > &M, const T eps=wolf::Constants::EPS)
Returns whether a matrix is square and symmetric (up to eps)
Definition: covariance.h:28
bool makePosDef(Eigen::Matrix< T, N, N, RC > &M, const T &eps=Constants::EPS)
Makes a symmetric matrix positive definite, raising its eigenvalues below eps. Returns whether it was...
Definition: covariance.h:68
bool isPositiveSemiDefinite(const Eigen::Matrix< T, N, N, RC > &M, const T &eps=Constants::EPS)
Returns whether all the eigenvalues of a symmetric matrix are greater or equal than eps.
Definition: covariance.h:37